import logging
import os
from logging import handlers

format = "%(asctime)s - %(name)s- %(filename)s[line:%(lineno)d] - %(levelname)s - %(message)s"
formatter = logging.Formatter(format, datefmt='%Y-%m-%d %H:%M:%S')
logging.basicConfig(
    format=format, level=logging.INFO
)

# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger('sqlalchemy.engine').setLevel(logging.DEBUG)

def getLogger(name=__name__):
    logger = logging.getLogger(name)
    normal_log_path = f'logs/info.log'
    debug_log_path = f'logs/debug.log'
    if not os.path.exists('./logs'):
        os.mkdir('./logs')
    set_logger(logger, normal_log_path, logging.INFO)
    set_logger(logger, debug_log_path, logging.DEBUG)
    
    return logger

def set_logger(logger, path, level):
    fh = handlers.TimedRotatingFileHandler(
        filename=path,
        when="D",
        interval=1,
        backupCount=3,
        encoding='utf-8'
    )
    fh.setLevel(level)
    fh.setFormatter(formatter)
    logger.addHandler(fh)
